package watchTracker;

import java.io.File;
import java.io.IOException;

public class fileWalker {
	private File movieFile;
	private File markerFile;
	private int movieFileIndex=0;
	private static final String markExtension = ".mrk";
	private static String[] childString;
	private static File oldMarkerFile;
	private static core coreRef;
	
	public fileWalker(core coreRef){
		this.coreRef = coreRef;
	}
	
	//trigger from gui to watch new show
	public void watchShowName(String fileName){
		makeFiles(movieFile.getParentFile().getAbsolutePath()+"\\"+fileName);
		startHash();
	}
	public void watchShowPath(String filePath){
		makeFiles(filePath);
		startHash();
	}

	
	private void makeFiles(String pathName){
		movieFile = new File(pathName);
		coreRef.setFilePathGUI(pathName);
		markerFile = new File(pathName+markExtension);
		
		//find old marker file and delete
		try {
			childString = movieFile.getParentFile().list();
			//step through files in folder
			for(int j = 0;j<childString.length;j++){
				
				//add file name to gui fileList
				coreRef.addFileGUI(childString[j]);
				if(childString[j].contains(movieFile.getName())){
					movieFileIndex=j+1;
					
				}
				
				//if marker file found try and delete it
				if(childString[j].contains(markExtension)){
					try{
						oldMarkerFile = new File(movieFile.getParent()+"\\"+childString[j]);
						oldMarkerFile.delete();
					}	
					catch(SecurityException exception){
						System.out.println(exception.getMessage());	
					}
				}
			}
		}
		catch(Exception exception){
			System.out.println(exception.getMessage());
		}
		
		
		// create the new marker file
		try {
			markerFile.createNewFile();
		}
		catch(IOException exception){
			System.out.println(exception.getMessage());
		}
		
		
		//open the movie
		if(movieFile.exists()){
			try{
				Runtime.getRuntime().exec("cmd /C start mpc-hc.exe \""+movieFile.toString()+"\"");
			}catch(Exception exception){
				exception.printStackTrace();
			}
		}
	}
	
	private void startHash(){
		
		//find season number
		if(movieFile.getParentFile().getName().contains("Season ")){
			int seasonNumb = Integer.parseInt(movieFile.getParentFile().getName().replace("Season ", ""));
			coreRef.setSeason(seasonNumb);
			String seasonString;
			if(seasonNumb < 10){
				seasonString = "S0"+seasonNumb+"E";
			}else {
				seasonString = "S"+seasonNumb+"E";
			}
			
			if(movieFile.getName().contains(seasonString)){
				int index;
				int episodeNumb;
				String subString;
				String episodeTitle;
				index = movieFile.getName().indexOf(seasonString);
				subString = movieFile.getName().substring(index+4, index+6);
				
				if(subString.charAt(0)== '0'){
					subString.replaceFirst("0", "");
				} 
				coreRef.setEpisode(Integer.parseInt(subString));
				episodeTitle =movieFile.getName().substring(index+6,movieFile.getName().length()-4);
				if(episodeTitle.charAt(0)==' '){
					episodeTitle.replaceFirst(" ",	"");
				}
				coreRef.setEpisodeTitle(episodeTitle);
				
			}
			
		}
		
		
		//find show name
		coreRef.setShowName(movieFile.getParentFile().getParentFile().getName());
		coreRef.setSelectedFileGUI(movieFileIndex);
	}
}
